//
//  PopoverAnimation.swift
//  XLWB
//
//  Created by 郭亮 on 16/3/15.
//  Copyright © 2016年 gl. All rights reserved.
//

import UIKit

class PopoverAnimation: NSObject {
    
    // MARK:-属性
    private var isPresented: Bool = false
    var presentedFrame: CGRect = CGRectZero
    var animationBlock: ((isPresented: Bool) -> ())?
}

// MARK:-UIViewControllerTransitioningDelegate
extension PopoverAnimation: UIViewControllerTransitioningDelegate {
    //改变弹出view的尺寸及添加蒙板
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
        let presentationVc = GLPresentationController(presentedViewController: presented, presentingViewController: presenting)
        presentationVc.presentedFrame = presentedFrame
        return presentationVc
    }
    
    //改变转场动画效果
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = true
        if animationBlock != nil {
        animationBlock!(isPresented: isPresented)
        }
        //返回代理
        return self
    }
    
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = false
        if animationBlock != nil {
        animationBlock!(isPresented: isPresented)
        }
        return self
    }
}

// MARK:-UIViewControllerAnimatedTransitioning
extension PopoverAnimation: UIViewControllerAnimatedTransitioning {
    //设置动画时间
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 1.0
    }
    
    //设置转场动画
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        isPresented ? presentedAnimation(transitionContext) : dismissAnimation(transitionContext)
    }
}

extension PopoverAnimation {
    /// 弹出视图动画
    private func presentedAnimation(transitionContext: UIViewControllerContextTransitioning) {
        //拿到弹出视图
        let presentedView = transitionContext.viewForKey(UITransitionContextToViewKey)!
        //如果存在弹出的view,添加到容器中
        transitionContext.containerView()?.addSubview(presentedView)
        //设置动画
        presentedView.transform = CGAffineTransformMakeScale(1.0, 0.0)
        presentedView.layer.anchorPoint = CGPoint(x: 0.5, y: 0.0)
        UIView.animateWithDuration(transitionDuration(transitionContext)) { () -> Void in
            //            presentedView.transform = CGAffineTransformMakeScale(1.0, 1.0)
            presentedView.transform = CGAffineTransformIdentity
        }
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            //            presentedView.transform = CGAffineTransformMakeScale(1.0, 1.0)
            presentedView.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                transitionContext.completeTransition(true)
        }
    }
    ///dismiss视图动画
    private func dismissAnimation(transitionContext: UIViewControllerContextTransitioning) {
        //拿到dismissView
        let dismissView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
        //设置动画
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            dismissView.transform = CGAffineTransformMakeScale(1.0, 0.001)
            }, completion: { (_) -> Void in
                dismissView.removeFromSuperview()
                transitionContext.completeTransition(true)
        })
    }
}